// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.event.MouseEvent;

public class PopupMenu extends Window
{
    protected Component parentMenu;
    protected int popupOrientation;
    
    public void add(MenuItem item)
    {
        item.setAlignment(H_EXPAND);
        item.parentMenu = this;
        if (item instanceof Menu)
        ((Menu) item).setPopupPlacement(popupOrientation);
        super.add(item);
    }

    public String getName()
    {
        return "PopupMenu";
    }
    
    /**
     * Returns an point which has been adjusted to take into account of the
     * desktop bounds
     */
    protected void adjustPopupLocationToFitScreen()
    {
        int px = x;
        int py = y;

        Rectangle screenBounds = new Rectangle(0, 0, UIManager.getCanvas().getWidth(), UIManager.getCanvas().getHeight());

        int scrWidth = screenBounds.width;
        int scrHeight = screenBounds.height;

        Dimension size = this.getPreferredSize();

        // Use long variables to prevent overflow
        int pw = px + size.width;
        int ph = py + size.height;

        if( pw > screenBounds.x + scrWidth )
             px = screenBounds.x + scrWidth - size.width;

        if( ph > screenBounds.y + scrHeight)
             py = screenBounds.y + scrHeight - size.height;

        /* Change is made to the desired (X,Y) values, when the
           PopupMenu is too tall OR too wide for the screen
        */
        if( px < screenBounds.x )
            px = screenBounds.x ;
        if( py < screenBounds.y )
            py = screenBounds.y;

        setLocation(px, py);
    }

    public void setVisible(boolean visible)
    {
        if (windowIsCreated)
        {
            if (visible)
            {
                adjustPopupLocationToFitScreen();
            }
            else
            {
                for (int i=0; i<components.size(); i++)
                {
                    Component c = (Component) components.elementAt(i);
                    if (c instanceof Menu)
                    {
                        Menu menu = (Menu) c;
                        if (menu.isOpened) menu.close();
                    }
                    if (c instanceof MenuItem)
                    {
                    }
                }

                this.prevEnteredComponent = null;
            }
            super.setVisible(visible);
        }
    }

    public Component getRoot()
    {
        Component root = this;

        if (parentMenu != null)
        {
            if (parentMenu instanceof Menu)
                root = ((Menu) parentMenu).getRoot();
        }

        return root;
    }

    public void setPopupOrientation(int popupOrientation)
    {
        this.popupOrientation = popupOrientation;
        for (int i=0; i<components.size(); i++)
        {
            Component c = (Component) components.elementAt(i);
            if (c instanceof Menu) ((Menu) c).setPopupPlacement(popupOrientation);
        }
    }

    public boolean transferFocusDownCycle()
    {
        Component selectedItem = UIManager.getFocusOwner();
        if (selectedItem != null && selectedItem.parent == this)
        {
            if (selectedItem instanceof Menu)
            {
                selectedItem.doClick();
                return true;
            }
            else if (selectedItem instanceof MenuItem)
            {
                Component root = getRoot();
                if (root != null)
                {
                    if (root instanceof Menu) ((Menu) root).close();
                    boolean b = root.transferFocus();
                    //Component newFocusOwner = UIManager.getFocusOwner();
                    //if (newFocusOwner != null && newFocusOwner instanceof Menu)
                    //    ((Menu) newFocusOwner).open();
                    return b;
                }
            }
        }

        return false;
    }
   

}


